/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2018 All Rights Reserved.
 */
package com.sgzn.sgpay.service.support;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.sgzn.sgpay.enums.EnumDeviceState;
import com.sgzn.sgpay.model.response.DeviceStatisticDto;
import com.sgzn.sgpay.persistence.dao.TDeviceMapper;
import com.sgzn.sgpay.persistence.domain.TDevice;
import com.sgzn.sgpay.persistence.domain.TDeviceExample;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备相关统计处理器
 * @author zhongzhongyong
 * @version Id: DeviceStatHandler.java, v 0.1 2018/5/17 23:49 zhongzhongyong Exp $$
 */
@Component
@Slf4j
public class DeviceStatHandler {

    @Resource
    private TDeviceMapper tDeviceMapper;

    /**
     * 根据商户ID获取设备统计数据
     * @param mchId
     * @return
     */
    public DeviceStatisticDto getDeviceStatisData(String mchId,int currentPage){
        DeviceStatisticDto dto = null;
        if(StringUtils.isNotBlank(mchId) && currentPage == 1){
            dto = new DeviceStatisticDto();
            TDeviceExample example = new TDeviceExample();
            TDeviceExample.Criteria cra = example.createCriteria();
            cra.andMerchantIdEqualTo(mchId);
            List<TDevice> list = tDeviceMapper.selectByExample(example);
            int allNum = 0;
            int onlineNum = 0;
            int offlineNum = 0;
            int shopNum = 0;
            Map<String,String> shopMap = new HashMap<>();
            if(CollectionUtils.isNotEmpty(list)){
                allNum = list.size();
                for(TDevice item : list){
                    if(EnumDeviceState.ONLINE.getStateCode().equals(item.getState())){
                        onlineNum++ ;
                    }else{
                        offlineNum++;
                    }
                    //使用Map去重
                    shopMap.put(item.getShopId(),item.getShopId());
                }
                shopNum = shopMap.size();
            }
            dto.setAllNum(allNum);
            dto.setOfflineNum(offlineNum);
            dto.setOnlineNum(onlineNum);
            dto.setShopNum(shopNum);
        }
        return dto;
    }


    /**
     * 根据商户ID、门店ID获取设备统计数据
     * @param mchId
     * @return
     */
    public DeviceStatisticDto getDeviceStatisData(String mchId,String shopId){
        DeviceStatisticDto dto = null;
        if(StringUtils.isNotBlank(mchId) && StringUtils.isNotBlank(shopId)){
            dto = new DeviceStatisticDto();
            TDeviceExample example = new TDeviceExample();
            TDeviceExample.Criteria cra = example.createCriteria();
            cra.andMerchantIdEqualTo(mchId).andShopIdEqualTo(shopId);
            List<TDevice> list = tDeviceMapper.selectByExample(example);
            int allNum = 0;
            int onlineNum = 0;
            int offlineNum = 0;
            if(CollectionUtils.isNotEmpty(list)){
                allNum = list.size();
                for(TDevice item : list){
                    if(EnumDeviceState.ONLINE.getStateCode().equals(item.getState())){
                        onlineNum++ ;
                    }else{
                        offlineNum++;
                    }
                }
            }
            dto.setAllNum(allNum);
            dto.setOfflineNum(offlineNum);
            dto.setOnlineNum(onlineNum);
        }
        return dto;
    }
}
